package cn.yangyumanage.fragment;

import android.app.Activity;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.wang.avi.AVLoadingIndicatorView;

import java.util.List;

import cn.yangyumanage.R;
import cn.yangyumanage.adapter.DouBanSearchRvAdapter;
import cn.yangyumanage.bean.DouBanSearchItemBean;
import cn.yangyumanage.callback.DataCallBack;
import cn.yangyumanage.http.HttpClient;
import cn.yangyumanage.model.DouBanSearchHttpModel;
import cn.yangyumanage.util.Utils;
import cn.yangyumanage.util.ViewUtil;


public class DouBanSearchFragment extends Fragment implements TextView.OnEditorActionListener, View.OnClickListener {

    private Activity activity;
    private View view;
    private EditText et_search;
    private TextView tv_search;
    private RecyclerView rv_search;
    private ViewPager2 viewPager2;
    private FilmAddFragment filmAddFragment;

    private AVLoadingIndicatorView aiv_douban_search;


    public DouBanSearchFragment(Activity activity, ViewPager2 viewPager2, FilmAddFragment filmAddFragment, AVLoadingIndicatorView aiv_douban_search) {
        this.activity = activity;
        this.viewPager2 = viewPager2;
        this.filmAddFragment = filmAddFragment;
        this.aiv_douban_search = aiv_douban_search;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_dou_ban_search, container, false);
        initView();
        return view;
    }

    private void initView() {
        et_search = view.findViewById(R.id.et_search);
        tv_search = view.findViewById(R.id.tv_search);
        rv_search = view.findViewById(R.id.rv_search);
        et_search.setOnEditorActionListener(this);
        tv_search.setOnClickListener(this);
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        //监听搜索键
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            String searchContent = et_search.getText().toString();
            ViewUtil.hideOneInputMethod(activity, et_search);
            if (searchContent.equals("")) {
                et_search.clearFocus();
                return false;
            }
            aiv_douban_search.smoothToShow();
            getSearchContent(searchContent);
            et_search.clearFocus();
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_search:
                String searchContent = et_search.getText().toString();
                ViewUtil.hideOneInputMethod(activity, et_search);
                if (searchContent.equals("")) {
                    et_search.clearFocus();
                    break;
                }
                aiv_douban_search.smoothToShow();
                getSearchContent(searchContent);
                et_search.clearFocus();
                break;
        }
    }

    //获取搜索内容（豆瓣）
    private void getSearchContent(String searchContent) {
        DouBanSearchHttpModel model = new DouBanSearchHttpModel();
        model.getSearchContent(searchContent, new DataCallBack() {
            @Override
            public void getData(Object data) {
                List<DouBanSearchItemBean> datas = Utils.<DouBanSearchItemBean>objectToList(data);
                HttpClient.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        DouBanSearchRvAdapter adapter = new DouBanSearchRvAdapter(activity, datas, viewPager2, filmAddFragment);
                        rv_search.setAdapter(adapter);
                        rv_search.setLayoutManager(new LinearLayoutManager(activity));
                        aiv_douban_search.smoothToHide();
                    }
                });
            }

        });
    }
}